package cn.hesion.leetcode;
/**
 * ClassName: LongestNiceSubstring <br/>
 * Description: <br/>
 * date: 2022/6/6 9:08<br/>
 *
 * @author Hesion<br />
 * @version
 * @since JDK 1.8
 */

/**
 * 1763. 最长的美好子字符串
 * @author: hesion
 * @create: 2022-06-06 09:08
 **/
public class LongestNiceSubstring {
    public int code(char c){
        if(c>='a' && c <= 'z'){
            return c -'a';
        }
        return c - 'A'+26;
    }
    public String longestNiceSubstring(String s) {
        int n = s.length();
        int l,i,j,k;
        int[]  hash ;
        for(l = n;l>0;--l){
            i = 0;
            j = -1;
            hash = new int[52];
            while(j < n-1){
                ++j;
                ++hash[code(s.charAt(j))];
                while(j-i+1 > l){
                    --hash[code(s.charAt(i))];
                    ++i;
                }

                if(j-i+1 == l){
                    for(k=0;k<26;++k){
                        if(hash[k]>=1&&hash[k+26]<=0){
                            break;
                        }else if(hash[k]<=0&&hash[k+26]>=1){
                            break;
                        }
                    }
                    if(k==26){
                        String tmp =  s.substring(i,i+j-i+1);
                        return tmp;
                    }
                }

            }

        }
        return "";
    }

    public static void main(String[] args) {
        String yazaAay = new LongestNiceSubstring().longestNiceSubstring("YazaAay");
        System.out.println(yazaAay);
    }
}
